//
//  WXPopView.m
//  CarJX
//
//  Created by xcq on 14-9-20.
//  Copyright (c) 2014年 KK. All rights reserved.
//
#import "WXPopViewTableViewCell.h"
#import "WXPopView.h"
#define kCellHeight 44
 #define kPopViewOrigin   CGPointMake(45, 210)
#define kPopViewWidth 230
#define kCellIdentifier @"popCell"
static int currenNum = 1;
static NSString *removeKey  = @"0";

@implementation WXPopView{
    
    
    
    
    NSString *_seleteOnekey;
    NSString *_seleteKey;
}

-(id) initWithCollectionFrame:(CGRect)frame andDataSource:(NSDictionary *)datesource{
    self = [super initWithFrame:frame];
    if (self) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        [_layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        _collection = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:_layout];
        _collection.delegate = self;
        _collection.dataSource = self;
        [_collection registerNib:[UINib nibWithNibName:@"WXCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCellIdentifier];
    
        [self addSubview:_collection];
    }
    return self;
}

- (id)initWithTableViewFrame:(CGRect)frame andDataSource:(NSDictionary *)datesource andClickNum:(int) clickNum{
    self = [super initWithFrame:frame];
    if (self) {
        
        if (clickNum == 1) {
            _clickNum = 1;
        }else{
        
#pragma mark 需求出现2级菜单，只需要把_clickNum修改为2
        _clickNum = 3;
        }
        [self initDataSource:datesource];
        if (clickNum == 2) {
            NSInteger height = [[datesource objectForKey:@"server"] count] * kCellHeight;
            CGRect tableRect = (CGRect){ kPopViewOrigin , {kPopViewWidth,height}};
            _tableView = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStylePlain];

        }else{
            NSInteger height = [_mapDistanceArray count] * kCellHeight;
            CGRect tableRect = (CGRect){ kPopViewOrigin , {kPopViewWidth,height}};

            _tableView = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStylePlain];

        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [_tableView registerNib:[UINib nibWithNibName:@"WXPopViewTableViewCell" bundle:nil] forCellReuseIdentifier:kCellIdentifier];
        [self addSubview:_tableView];
        [_tableView reloadData];
    }
    return self;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    currenNum = 1;
    
    
    if ([removeKey isEqualToString: @"0"] && currenNum == 2) {
         removeKey = @"1";
    }
    [self removeFromSuperview];
    
}



#pragma mark 数据源数据第一次处理
- (void) initDataSource:(NSDictionary *) dic{
    if(_serverArrName_one == nil){
        _serverArrName_one = [[NSMutableArray alloc] init];
    }
    if (_serverArrName_two == nil) {
        _serverArrName_two = [[NSMutableArray alloc] init];
    }
    
    if (_serverDid_two == nil) {
        _serverDic_one = [[NSMutableDictionary alloc] init];
        _serverDid_two = [[NSMutableDictionary alloc] init];
    }
    if (_clickNum == 2 || _clickNum == 3)
        {_serverArr_one = [dic objectForKey:@"server"];

         for (NSDictionary *dic in _serverArr_one) {
//             [_serverDid_two setObject:[dic objectForKey:@"SEQ"] forKey: [dic objectForKey:@"DIC_VALUE"]];
         }
    
        for (NSDictionary *dic_name in _serverArr_one)
        {
             [_serverArrName_one addObject: [dic_name objectForKey:@"DIC_VALUE"]];
        }
    }else if(_clickNum == 1){
        _mapDistanceArray = (NSMutableArray *)@[@"1km",@"3km", @"5km" , @"10km"];
    }
    
}

#pragma mark 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   //如果只有一个菜单
    if (_clickNum == 1 && currenNum == 1) {
 
        return [_mapDistanceArray count];
    
    }else if (currenNum == 1 && ( _clickNum == 2 ||_clickNum == 3)){
 
        return [_serverArr_one count];
    }else{
        if (currenNum == 2  && _clickNum == 2  && _serverArr_two == nil) {
            return [_serverArr_one count];
        }
        
 
        return  [_serverArr_two count];
    }
    
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WXPopViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    

    if (_clickNum == 1 && currenNum == 1) {
       
         cell.popLabel.text = [_mapDistanceArray objectAtIndex:indexPath.row];
    }else if (currenNum == 1 && ( _clickNum == 2 || _clickNum == 3)){
 
        cell.popLabel.text = [_serverArrName_one objectAtIndex:indexPath.row];
    }else{
        if ([removeKey isEqualToString:@"1"] && currenNum == 2 && _clickNum == 2) {
            cell.popLabel.text = [_serverArrName_one objectAtIndex:indexPath.row];
            removeKey = @"0";
        }
        if ([_serverArrName_two count ] == 0) {
            cell.popLabel.text = [_serverArrName_one objectAtIndex:indexPath.row];

        }else{
            cell.popLabel.text = [_serverArrName_two objectAtIndex:indexPath.row];

        }
        
        
    }
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //第二次点击传入内容
    if(currenNum == 2 && _clickNum == 2){
        //点击传入内容
         NSDictionary *seleteDic = [_serverArr_two objectAtIndex:indexPath.row];
        _seleteKey = [seleteDic objectForKey:@"DIC_NAME"];
        _CellClickBlock(_seleteKey,_seleteOnekey);
        currenNum = 1;
        
        [UIView animateWithDuration:0.5f animations:^{
            [self removeFromSuperview];

        }];
    }else if (currenNum == 1 && _clickNum == 3) {    //第一次点击切换数据源

#pragma mark 修改这里
    //  NSString *str = [_serverArrName_one objectAtIndex:indexPath.row];
        
        NSDictionary *serverOne = [self.serverArr_one objectAtIndex:indexPath.row];
        //返回的字段
        _seleteOnekey = [serverOne objectForKey:@"DIC_NAME"];
        
        _CellClickBlock(_seleteKey,_seleteOnekey);
        currenNum = 1;
        
        [UIView animateWithDuration:0.5f animations:^{
            [self removeFromSuperview];
            
        }];
//        if (_serverArrName_two == nil) {
//            _serverArrName_two = [[NSMutableArray alloc] init];
//        }
//        _serverArr_two = [_serverDid_two objectForKey:str];
//        NSDictionary *seleteDic = [_serverArr_two objectAtIndex:indexPath.row];
//        _seleteOnekey = [seleteDic objectForKey:@"DIC_NAME"];
//        
//        for (NSDictionary *dic  in  _serverArr_two) {
//            [_serverArrName_two addObject: [dic objectForKey:@"DIC_VALUE"]];
//        }
//        NSInteger tableHeight = [_serverArrName_two count] * kCellHeight;
//        NSInteger currenHeight = tableView.frame.size.height;
//        NSInteger result = fabsf(currenHeight - tableHeight);
//        [UIView animateWithDuration:0.5f animations:^{
//            tableView.frame = (CGRect){{kPopViewOrigin.x ,kPopViewOrigin.y - (result /5)},kPopViewWidth,currenHeight + result / 5};
//        }];
//        currenNum ++;
//        [tableView reloadData];
    }else if(currenNum == 1 && _clickNum == 1){
    //菜单只有一类的时候传入
        _CellDistanceClickBlock(_mapDistanceArray[indexPath.row]);
        [UIView animateWithDuration:0.5f animations:^{
            [self removeFromSuperview];
            
        }];
    }
    
    
}

@end
